<template>
  <a-card class="body-container">
    <s-list-form>
      <template #less>
        <s-form-item size="list-middle" label="模板分组">
          <s-select
            v-model="query.groupId"
            placeholder="模板分组"
            url="/code/template/groups"
            allow-clear
          />
        </s-form-item>
        <s-form-item size="list-middle" label="模板类型">
          <s-select
            v-model="query.templateType"
            placeholder="模板类型"
            code="TEMPLATE_TYPE"
            allow-clear
          />
        </s-form-item>
        <s-form-item size="list-middle" label="名称">
          <s-input v-model="query.templateName" placeholder="名称" allow-clear />
        </s-form-item>
      </template>
      <template #more />
    </s-list-form>
    <s-tool-bar>
      <template #left>
        <s-button
          v-if="authorize.insert"
          type="primary"
          icon="plus-outlined"
          @click="$refs.add.open()"
          >新增</s-button
        >
        <a-popconfirm
          v-if="authorize.deleteMulti"
          title="确认删除所选数据？"
          ok-text="是"
          cancel-text="否"
          @confirm="deleteMulti"
        >
          <s-button type="danger" icon="delete-outlined">删除</s-button>
        </a-popconfirm>
      </template>
      <template #right>
        <s-button type="primary" icon="search-outlined" @click="$refs.table.search(query)"
          >查询</s-button
        >
        <s-button style="margin-left: 8px" icon="stop-outlined" @click="query = {}">重置</s-button>
      </template>
    </s-tool-bar>
    <s-table
      ref="table"
      bordered
      :scroll="{ y: tableHeight }"
      :columns="columns"
      url="/code/template/pageList"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a
            v-if="authorize.selectOneById"
            style="margin-right: 8px"
            @click="$refs.detail.open(record.id)"
          >
            <s-icon type="eye-outlined" />查看
          </a>
          <a
            v-if="authorize.update"
            style="margin-right: 8px"
            @click="$refs.update.open(record.id)"
          >
            <s-icon type="edit-outlined" />编辑
          </a>
          <a style="margin-right: 8px" @click="$refs.templateData.open(record.id)">
            <s-icon type="edit-outlined" />模板设置
          </a>
          <a-popconfirm
            v-if="authorize.delete"
            title="确认删除此条数据？"
            ok-text="是"
            cancel-text="否"
            @confirm="deleteById(record.id)"
          >
            <a style="margin-right: 8px"> <s-icon type="delete-outlined" />删除 </a>
          </a-popconfirm>
        </template></template
      >
    </s-table>
    <detail ref="detail" />
    <add
      ref="add"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />
    <update
      ref="update"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />
    <template-data ref="templateData" />
  </a-card>
</template>

<script>
const columns = [
  {
    title: '模板名称',
    dataIndex: 'templateName',
    ellipsis: true,
    sorter: true,
    width: 200
  },
  {
    title: '组名',
    dataIndex: 'groupName',
    ellipsis: true,
    width: 200
  },
  {
    title: '组编号',
    dataIndex: 'groupId',
    ellipsis: true,
    width: 200
  },
  {
    title: '模板类型',
    dataIndex: 'templateTypeName',
    ellipsis: true,
    width: 200
  },
  {
    title: '路径',
    dataIndex: 'path',
    ellipsis: true,
    width: 400
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'action',
    width: 300
  }
]
import Detail from './detail/index.vue'
import Add from './add/index.vue'
import Update from './update/index.vue'
import TemplateData from './templateData/index.vue'
export default {
  name: 'QueryList',
  components: { Detail, Add, Update, TemplateData },
  data() {
    return {
      query: {},
      columns: columns
    }
  },
  authorize: '/code/template/permissionList',
  methods: {
    deleteMulti() {
      const keys = this.$refs.table.getSelectedKeys()
      if (keys.length > 0) {
        this.$loading(true)
        this.$api
          .DELETE('/code/template/deleteMulti', { ids: keys.toString() })
          .then(() => {
            this.$loading(false)
            this.$refs.table.search(this.query)
          })
          .catch(() => {
            this.$loading(false)
          })
      }
    },
    deleteById(id) {
      this.$loading(true)
      this.$api
        .DELETE('/code/template/delete', { id: id })
        .then(() => {
          this.$loading(false)
          this.$refs.table.search(this.query)
        })
        .catch(() => {
          this.$loading(false)
        })
    }
  }
}
</script>

<style lang="less" scoped></style>
